const { AsyncSeriesLoopHook } = require('tapable')

let hook = new AsyncSeriesLoopHook(['name'])

let count = 0
console.time('time')
hook.tapPromise('fn1', function (name) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log(`fn1 => ${name}`)
      resolve()
    }, 1000)
  })
})

hook.tapPromise('fn2', function (name) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log(`fn2 => ${name}`)
      if (++count === 2) {
        count = 0
        resolve()
      }
      resolve('1')
    }, 2000)
  })
})

hook.tapPromise('fn3', function (name) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log(`fn3 => ${name}`)
      resolve()
    }, 3000)
  })
})

hook.promise('yhzzy').then(function (msg) {
  console.timeEnd('time')
})